package com.onlyphoto.onlymall.mvp.view.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.bean.response.GoodsBean
import com.onlyphoto.onlymall.mvp.view.viewholder.GoodsItemHolder
import com.onlyphoto.onlymall.mvp.view.viewholder.SubTitleHolder
import java.util.ArrayList

/**
 * Created by gaochujia on 2019-07-12.
 */

class GoodsAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var list = arrayListOf<GoodsBean>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            TYPE_TITLE -> {
                val item = LayoutInflater.from(parent.context).inflate(
                        R.layout.subtitle_goods, parent, false)
                SubTitleHolder(item)
            }
            TYPE_ITEM -> {
                val item = LayoutInflater.from(parent.context).inflate(
                        R.layout.item_goods, parent, false)
                GoodsItemHolder(item)
            }
            else -> null
        }!!

    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (getItemViewType(position)) {
            TYPE_TITLE ->
                (holder as SubTitleHolder).loadData(list[position])
            TYPE_ITEM ->
                (holder as GoodsItemHolder).loadData(list[position])

        }

    }

    override fun getItemViewType(position: Int): Int {
        return if (list[position].isSubtitle) {
            TYPE_TITLE
        } else {
            TYPE_ITEM
        }
    }

    fun setItemData(list: ArrayList<GoodsBean>) {
        this.list.clear()
        val bean = GoodsBean(true, "", 0, "猜你喜欢",
                0.0, 0.0, 0, 0,"")
        this.list.add(bean)
        for (goodsBean in list){
            this.list.add(goodsBean)
        }
        notifyDataSetChanged()
    }

    companion object {
        const val TYPE_TITLE = 1
        const val TYPE_ITEM = 2
    }
}